package com.zretc.device.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.zretc.device.dao.Device_AdminDao;
import com.zretc.device.dao.UserDao;
import com.zretc.device.entity.Device_Admin;
import com.zretc.device.entity.User;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ModPassDevAdmin extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtusername;
	private JPasswordField txtpassword;
	private JPasswordField txtpassconfirm;
	private JPasswordField txtBadPass;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			ModPassDevAdmin dialog = new ModPassDevAdmin();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public ModPassDevAdmin() {
		setBounds(100, 100, 632, 521);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JLabel label = new JLabel("修改密码");
		label.setFont(new Font("宋体", Font.PLAIN, 40));
		label.setBounds(197, 25, 167, 47);
		contentPanel.add(label);

		JLabel label_1 = new JLabel("用 户 名");
		label_1.setFont(new Font("宋体", Font.PLAIN, 25));
		label_1.setBounds(110, 129, 101, 30);
		contentPanel.add(label_1);

		txtusername = new JTextField();
		txtusername.setEditable(false);
		txtusername.setFont(new Font("宋体", Font.PLAIN, 25));
		txtusername.setColumns(10);
		txtusername.setBounds(241, 129, 250, 30);
		contentPanel.add(txtusername);

		JLabel label_3 = new JLabel("设置新密码");
		label_3.setFont(new Font("宋体", Font.PLAIN, 25));
		label_3.setBounds(86, 215, 125, 30);
		contentPanel.add(label_3);

		txtpassword = new JPasswordField();
		txtpassword.setBounds(241, 215, 250, 30);
		contentPanel.add(txtpassword);

		txtpassconfirm = new JPasswordField();
		txtpassconfirm.setBounds(241, 258, 250, 30);
		contentPanel.add(txtpassconfirm);

		JLabel label_4 = new JLabel("确认新密码");
		label_4.setFont(new Font("宋体", Font.PLAIN, 25));
		label_4.setBounds(86, 258, 125, 30);
		contentPanel.add(label_4);

		JButton button = new JButton("修改");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//取值
				String username = txtusername.getText();
				char [] cbadpass=txtBadPass.getPassword();
				char [] cpassword = txtpassword.getPassword();
				char [] cpassConfirm = txtpassconfirm.getPassword();
				//转型
				String badpass=new String(cbadpass);
				String password = new String(cpassword);
				String passConfirm = new String(cpassConfirm);
				if (password.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "密码不能为空");
				}else if(passConfirm.equals("")){
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "密码不能为空");
				}else {
					Device_AdminDao dad = new Device_AdminDao();
					boolean flag1=dad.dologin(username, badpass);
					if (flag1) {
						if (password.equals(passConfirm)) {
							Device_Admin dAdmin = new Device_Admin(username, null, password);
							//调用修改密码的方法
							boolean flag=dad.modDevAdmin(dAdmin);
							if (flag) {
								// 弹窗提示
								JOptionPane.showMessageDialog(null, "修改成功");
							}else {
								// 弹窗提示
								JOptionPane.showMessageDialog(null, "修改失败");
							}
						}else {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "新密码错误");
						}
					}else {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "原密码错误，修改失败");
					}
					
				}
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 25));
		button.setBounds(328, 326, 150, 38);
		contentPanel.add(button);

		JButton button_1 = new JButton("取消");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtBadPass.setText("");
				txtpassword.setText("");
				txtpassconfirm.setText("");
			}
		});
		button_1.setFont(new Font("宋体", Font.PLAIN, 25));
		button_1.setBounds(136, 326, 150, 38);
		contentPanel.add(button_1);
		
		JLabel label_2 = new JLabel("请输入原密码");
		label_2.setFont(new Font("宋体", Font.PLAIN, 25));
		label_2.setBounds(61, 172, 150, 30);
		contentPanel.add(label_2);
		
		txtBadPass = new JPasswordField();
		txtBadPass.setBounds(241, 172, 250, 30);
		contentPanel.add(txtBadPass);
		
		//给用户名赋值
		User user=User.getInfo();
		txtusername.setText(user.getUsername());
		// 居中
		setLocationRelativeTo(null);
	}

	/**
	 * Create the dialog.
	 */
	public ModPassDevAdmin(String username) {
		setBounds(100, 100, 632, 472);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JLabel label = new JLabel("修改密码");
		label.setFont(new Font("宋体", Font.PLAIN, 40));
		label.setBounds(197, 25, 167, 47);
		contentPanel.add(label);

		JLabel label_1 = new JLabel("用 户 名");
		label_1.setFont(new Font("宋体", Font.PLAIN, 25));
		label_1.setBounds(110, 129, 101, 30);
		contentPanel.add(label_1);

		txtusername = new JTextField();
		// 对值进行接收
		txtusername.setText(username);
		// 设置为不可编辑
		txtusername.setEditable(false);
		txtusername.setFont(new Font("宋体", Font.PLAIN, 25));
		txtusername.setColumns(10);
		txtusername.setBounds(241, 129, 250, 30);
		contentPanel.add(txtusername);

		JLabel label_3 = new JLabel("密    码");
		label_3.setFont(new Font("宋体", Font.PLAIN, 25));
		label_3.setBounds(107, 185, 120, 30);
		contentPanel.add(label_3);

		txtpassword = new JPasswordField();
		txtpassword.setBounds(241, 185, 250, 30);
		contentPanel.add(txtpassword);

		txtpassconfirm = new JPasswordField();
		txtpassconfirm.setBounds(241, 235, 250, 30);
		contentPanel.add(txtpassconfirm);

		JLabel label_4 = new JLabel("确认密码");
		label_4.setFont(new Font("宋体", Font.PLAIN, 25));
		label_4.setBounds(107, 235, 120, 30);
		contentPanel.add(label_4);

		JButton button = new JButton("修改");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//取值
				String username = txtusername.getText();
				char [] cpassword = txtpassword.getPassword();
				char [] cpassConfirm = txtpassconfirm.getPassword();
				//转型
				String password = new String(cpassword);
				String passConfirm = new String(cpassConfirm);
				if (password.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "密码不能为空");
				}else if(passConfirm.equals("")){
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "密码不能为空");
				}else {
					if (password.equals(passConfirm)) {
						Device_Admin dAdmin = new Device_Admin(username, null, password);
						Device_AdminDao dad = new Device_AdminDao();
						//调用修改密码的方法
						boolean flag=dad.modDevAdmin(dAdmin);
						if (flag) {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "修改成功");
						}else {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "修改失败");
						}
					}
				}
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 25));
		button.setBounds(332, 305, 150, 38);
		contentPanel.add(button);

		JButton button_1 = new JButton("返回");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
		button_1.setFont(new Font("宋体", Font.PLAIN, 25));
		button_1.setBounds(131, 305, 150, 38);
		contentPanel.add(button_1);
		// 居中
		setLocationRelativeTo(null);


	}

}
